package com.ruoyi.quartz.task;

import com.github.tusharepro.core.TushareProService;
import com.github.tusharepro.core.bean.StockBasic;
import com.github.tusharepro.core.entity.StockBasicEntity;
import com.github.tusharepro.core.http.Request;
import com.ruoyi.quartz.util.TushareUtil;
import com.ruoyi.stock.service.IStockBasicService;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

/** 股票列表同步任务 */
@Component("stockBasicTask")
@Slf4j
public class StockBasicTask {

  @Autowired private IStockBasicService stockBasicService;

  @Autowired private MongoTemplate mongoTemplate;

  private static final String COLLECTION_NAME = "stockBasic";

  public void run() {
    try {
      List<StockBasicEntity> stockBasicEntities = stockList();
      DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
      stockBasicEntities.forEach(
          model -> {
            log.info("save stock basic code :{}", model.getTsCode());
            List<com.ruoyi.stock.domain.StockBasic> stockBasics =
                stockBasicService.selectStockBasicList(
                    com.ruoyi.stock.domain.StockBasic.builder().tsCode(model.getTsCode()).build());
            if (CollectionUtils.isEmpty(stockBasics)) {
              com.ruoyi.stock.domain.StockBasic build =
                  com.ruoyi.stock.domain.StockBasic.builder()
                      .tsCode(model.getTsCode())
                      .symbol(model.getSymbol())
                      .name(model.getName())
                      .fullName(model.getFullname())
                      .enName(model.getEnname())
                      .area(model.getArea())
                      .industry(model.getIndustry())
                      .market(model.getMarket())
                      .exchange(model.getExchange())
                      .publishDate(
                          model.getListDate() == null ? null : yyyyMMdd.format(model.getListDate()))
                      .delistDate(
                          model.getDelistDate() == null
                              ? null
                              : yyyyMMdd.format(model.getDelistDate()))
                      .isHs(model.getIsHs())
                      .build();
              stockBasicService.insertStockBasic(build);
              mongoTemplate.save(build, COLLECTION_NAME);
            } else {
              mongoTemplate.save(stockBasics.get(0), COLLECTION_NAME);
            }
          });

    } catch (IOException e) {
      log.error(e.getMessage(), e);
    }
  }

  /** 股票列表接口 */
  private static List<StockBasicEntity> stockList() throws IOException {
    return TushareProService.stockBasic(
        new Request<StockBasicEntity>(TushareUtil.getBuilder().copy().build()) {}.allFields()
            //                        .param(StockBasic.Params.exchange.value("SZSE"))
            .param(StockBasic.Params.list_status.value("L")));
  }
}
